class Solution
{
public:
    string truncateSentence(string s, int k)
    {
        int i = 0;
        int n = s.size();
        int wordCount = 0;
        while (i < n)
        {
            if (s[i] == ' ')
            {
                ++wordCount;
                if (wordCount == k)
                {
                    break;
                }
            }
            ++i;
        }
        return {s.begin(), s.begin() + i};
    }
};